package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * DishServiceImpl 实体类
 *
 * @author Xiaolaodi
 * @description
 * @create 2025/5/17
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

	@Autowired
	private DishMapper dishMapper;
	@Autowired
	private DishFlavorMapper dishFlavorMapper;
	@Autowired
	private SetmealDishMapper setmealDishMapper;

	/**
	 * 新增菜品和对应的口味数据（可能有多个）
	 * @param dishDTO
	 */
	//涉及到多表操作，事务注解
	@Transactional
	public void saveWithFlavor(DishDTO dishDTO){
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO,dish);
		//向菜品表插入一条数据
		dishMapper.insert(dish);
		//此时本来拿不到dish_id，正在新增菜品没有id,所以要在Insert里配置返回主键语句
		Long dishId = dish.getId();
		//向口味表插入N条数据
		List<DishFlavor> flavors = dishDTO.getFlavors();
		if(flavors != null && !flavors.isEmpty()){
			flavors.forEach(flavor -> {
				flavor.setDishId(dishId);
			});
			dishFlavorMapper.insertBatch(flavors);
		}
	}
	/**
	 * 菜品分页查询
	 * @param dishPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
		PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
		Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);

		return new PageResult(page.getTotal(),page.getResult());
	}

	/**
	 * 菜品批量删除
	 * @param ids
	 */
	@Transactional
	//涉及到多表操作，事务注解
	public void deleteBatch(List<Long> ids) {
		//判断当前菜品是否能够删除：1. 启售中不可删除->业务异常
		//                     2. 与套餐setmeal关联，不可删除
		for(Long id : ids){
			Dish dish = new Dish();
			if(dishMapper.getById(id).getStatus()==1){
				throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
			}
		}
		List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
		if(setmealIds != null && !setmealIds.isEmpty()){
			throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
		}

		for (Long id : ids) {
			//删除菜品
			dishMapper.deleteById(id);
			//删除对应口味
			dishFlavorMapper.deleteByDishId(id);
		}
	}

	/**
	 * 菜品信息修改
	 * @param dishDTO
	 */
	public void updateWithFlavor(DishDTO dishDTO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishDTO, dish);
		dishMapper.update(dish);
		//口味数据：先全部删除再插入
		dishFlavorMapper.deleteByDishId(dishDTO.getId());
		List<DishFlavor> flavors = dishDTO.getFlavors();
		if (flavors != null && !flavors.isEmpty()) {
			flavors.forEach(flavor -> {
				flavor.setDishId(dishDTO.getId());
			});
			dishFlavorMapper.insertBatch(flavors);
		}
	}

	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	public DishVO findByIdWithFlavor(Long id) {
		//查询
		Dish dish = dishMapper.getById(id);

		List<DishFlavor> flavors = dishFlavorMapper.findByDishId(id);
		//封装成VO
		DishVO dishVO = new DishVO();
		BeanUtils.copyProperties(dish,dishVO);
		dishVO.setFlavors(flavors);
		return dishVO;
	}
}
